#!/bin/bash

# 此脚本修复嵌套链接问题，不需要恢复模式

echo "检查是否存在嵌套链接..."

# 首先，检查是否存在嵌套链接
if [ -L "/Applications/Applications" ]; then
    echo "发现嵌套链接，尝试移除..."
    sudo rm -f "/Applications/Applications"
    echo "嵌套链接已移除"
fi

# 现在我们需要确保应用程序能在Finder中显示
echo "设置Finder应用程序查看选项..."

# 创建一个AppleScript来修复Finder设置
cat > /tmp/fix_finder.scpt << EOL
tell application "Finder"
    try
        set sysVers to system version of (system info)
        if sysVers starts with "12" then
            tell application "System Events" to tell process "Finder"
                try
                    click menu item "应用程序" of menu "前往" of menu bar 1
                    delay 1
                end try
            end tell
        end if
    end try
end tell
EOL

# 运行AppleScript
echo "刷新Finder视图..."
osascript /tmp/fix_finder.scpt

# 创建临时文件夹（非桌面位置）
echo "创建临时应用程序目录..."
mkdir -p ~/.mac_sync/temp_apps

# 添加别名指向真实应用
ln -s /Volumes/Sun/Applications/*.app ~/.mac_sync/temp_apps/

# 运行Finder刷新脚本
cat > /tmp/refresh_finder.scpt << EOL
tell application "Finder"
    try
        open folder ((path to home folder as text) & ".mac_sync:temp_apps")
        delay 1
        close window 1
    end try
end tell
EOL

osascript /tmp/refresh_finder.scpt

# 清理临时文件夹
rm -rf ~/.mac_sync/temp_apps

echo "完成！现在您应该能在Finder中看到应用程序了。"
echo "注意：如果问题仍然存在，您可能需要重启系统。" 